<template>
  <div class="goods">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/" class="mb">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a>商品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <!-- 搜索 -->

      <el-input v-model="queryInfo.query" placeholder="请输入内容">
        <el-button @click="chaHandel" slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <!-- 添加 -->
      <el-button class="tj" type="primary" @click="isAddUser=true">添加</el-button>
      <!-- 商品表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="#" width="40"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="address" label="创建时间">
            <template slot-scope="scope">
            <div>
              {{scope.row.add_time}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="del(scope.row.goods_id)" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[3, 5, 10]"
          :page-size="1"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      tableData: [],
      total: "",
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 3
      }
    };
  },
  methods: {
    async getlist() {
      const res = await this.$axios.get("goods", { params: this.queryInfo }); 
      // 表格数据
      this.tableData = res.data.data.goods;

      this.total = res.data.data.total;

      console.log("商品列表", this.tableData);
    },
    // 删除
    async del(id) {
 const res = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(error => error);

      if (res !== "confirm") {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      } else {
        await this.$axios.delete(`goods/${id}`);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.getlist();
      }

    },
    // 分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getlist();
    },
    // 分页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getlist();
    },
    // 搜索
    chaHandel() {
      this.getlist();
    }
  },
  mounted() {
    this.getlist();
  }
};
</script>

<style lang="scss" scoped>
.mb {
  margin-bottom: 20px;
}
.tj {
  margin-bottom: 10px;
  margin-left: 10px;
}
.el-input {
  width: 300px;
}
</style>